<!--JQUERY CHOSEN LIBRARY-->
<link rel="stylesheet" href="css/chosen.css" />
<script src="js/chosen.jquery.js" type="text/javascript"></script>

        <script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
				$("#tgl_masuk").datetimepicker({
					changeMonth: true,
				    changeYear: true,
				    showOtherMonths: true,
					dateFormat:'yy-mm-dd',
					timeFormat: 'hh:mm:ss',
			        hourGrid: 8,
			        minuteGrid: 10,		
				});
				
				$("#id_kode").change(function(){
					$.ajax({						
						url:'getKodeBB.php',
						dataType: "html",
						type:'POST',
						data:'id_kode='+$(this).val(),
						success: function(data) {
							var obj = $.parseJSON(data);						
							if(data=="[]"){
								$("#kode_bb").val('');
								$("#satuan").val('');	
								$('#nama_bb').val('');		
							}else{							
								$("#kode_bb").val(obj[0].kode);
								$("#satuan").val(obj[0].satuan);
								$("#nama_bb").val(obj[0].nama);
							}
						}
					})
				})
			} );
		</script>
<?php
if($_SESSION['bbmasuk']!=1){
	echo "Akses tidak diizinkan";
	exit();
}

$act = filter($_GET['act']);
$field = filter($_GET['field']);
$q = filter($_GET['q']);

if(empty($act)){	
?>		
					<!-- box / title -->
					<div class="title">
						<h5>Data Bahan Baku Masuk</h5>								
						<div class="search global-nav">
						  <form method="GET" class="form-search js-search-form" action="index.php?pages=bb_masuk" id="global-nav-search" style="width:350px">
								<input type="hidden" name="pages" value="bb_masuk">
								<select name="field" id="birthday_month" aria-label="Month" class="" >
									<option value="" <?php echo ($field=="")?"selected":"";?>>Cari Berdasarkan:</option>
									<option value="no_invoice" <?php echo ($field=="no_invoice")?"selected":"";?>>No Invoice</option>
									<option value="kode_bb" <?php echo ($field=="kode_bb")?"selected":"";?>>Kode BB</option>
                                    <option value="nama_bb" <?php echo ($field=="nama_bb")?"selected":"";?>>Nama BB</option>
									<option value="jumlah" <?php echo ($field=="jumlah")?"selected":"";?>>Jumlah</option>		
                                    <option value="satuan" <?php echo ($field=="satuan")?"selected":"";?>>Satuan</option>
									<option value="tgl_masuk" <?php echo ($field=="tgl_masuk")?"selected":"";?>>Tgl Masuk</option>										
								</select>

				            	<label class="visuallyhidden" for="search-query">Search query</label>
				            	<input class="search-input" type="text" id="search-query" placeholder="Search" name="q" value="<?php echo $q;?>">
				            	<span class="search-icon js-search-action">
				              		<button type="submit" class="icon nav-search" tabindex="-1">
				              			<span class="visuallyhidden">Search</span>
				              		</button>
				            	</span>
				            	<input disabled="disabled" class="search-input search-hinting-input" type="text" id="search-query-hint" autocomplete="off" spellcheck="false">				              					              
							</form>
						</div>
					</div>
					<!-- end box / title -->
					<div class="table">										
						<table>
							<thead>
								<tr>
									<th class="selected"><input type="checkbox" class="checkall" /></th>
									<th class="action">No.</th>
									<th class="left">No Invoice</th>
									<th>Nama Suplier</th>
                                	<th>Kode BB</th>
									<th>Nama BB</th>
									<th>Jumlah</th>
									<th>Satuan</th>
                                    <th>Tgl Masuk</th>
                              <th class="left">Keterangan</th>
									<th class="action" colspan="2"><a href="?pages=bb_masuk&act=edit" class="icon-plus-sign" title="Tambah Data Baru"></a></th>									
								</tr>
							</thead>
							<tbody>
							<?php			

							$q2=$q;
							if($field=='status'){
								$q2 = (strtolower($q)=='aktif')?'1':'0';
							}
							
							$addQuery='';


							if(!empty($field))
								$addQuery .= " WHERE $field LIKE '%$q2%' ";

							$batas=10;
							$pg=(int) $_GET['pg'];
							if(empty($pg))
							{
								$posisi=0;
								$pg=1;
							}
							else
							{
								$posisi = ($pg-1) * $batas;
							}
							$sqlSup1 = mysql_query("SELECT * FROM tb_bb $addQuery ORDER BY id_bb DESC LIMIT $posisi, $batas");
							
							$no=$posisi+1;
							while($dataSup = mysql_fetch_array($sqlSup1)){								

								$status = ($dataSup['status']==1)? "Aktif":"Non Aktif";
							?>
								<tr>
									<td class="selected"><input type="checkbox" /></td>
									<td class="center"><?php echo $no;?></td>
									<td><?php echo $dataSup['no_invoice'];?></td>
                                    <?php $sql=mysql_query("select * from tb_suplier where id_suplier='$dataSup[id_suplier]'");
										  $sql2=mysql_fetch_array($sql);
									?>
									<td><?php echo $sql2['nama'];?></td>
									<td><?php echo $dataSup['kode_bb'];?></td>
									<td><?php echo $dataSup['nama_bb'];?></td>
                                    <td><?php echo $dataSup['jumlah'];?></td>
                                    <td><?php echo $dataSup['satuan'];?></td>
                                    <td><?php echo $dataSup['tgl_masuk'];?></td>
									<td><?php echo $dataSup['ket'];?></td>	
									<td class="action"><a href="?pages=bb_masuk&act=edit&id=<?php echo $dataSup['id_bb'];?>" class="icon-edit" title="Ubah Data"></a></td>
									<td class="action">
										<a href="#" title="Hapus Data" onClick="if(confirm('Anda akan menghapus data ini?')) window.location='?pages=bb_masuk&act=del&id=<?php echo $dataSup['id_bb'];?>';" class="icon-trash"></a>
									</td>								
								</tr>
							<?php	
								$no++;				
							}

							$sqlSup2 = mysql_query("SELECT * FROM tb_bb $addQuery ORDER BY id_bb DESC");
							$jumlahData = mysql_num_rows($sqlSup2);

							$file="?pages=bb_masuk&field=$field&q=$q";
							$jmlhalaman=ceil($jumlahData/$batas);
	
							?>															
							</tbody>
						</table>
						<!-- pagination -->
						<div class="pagination pagination-left">
			  <div class="results">
							  <p><span><?php echo "Showing Results ".($posisi+1)."-".($no-1)." of $jumlahData";?></span></p><div class="report">
				  <form method="post" action="print_bb_masuk.php" >
					<table width="185" border="1">
  <tr>
    <td width="68">Pilih Bulan</td>
    <td width="101"><select name="bulan" id="bulan">
                                                                <option label="January" value="01" selected="selected">January</option>
                                                                <option label="February" value="02">February</option>
                                                                <option label="March" value="03">March</option>
                                                                <option label="April" value="04">April</option>
                                                                <option label="May" value="05">May</option>
                                                                <option label="June" value="06">June</option>
                                                                <option label="July" value="07">July</option>
                                                                <option label="August" value="08">August</option>
                                                                <option label="September" value="09">September</option>
                                                                <option label="October" value="10">October</option>
                                                                <option label="November" value="11">November</option>
                                                                <option label="December" value="12">December</option>
                                                              </select></td>
    </tr>
  <tr>
    <td colspan="2" align="center"><span class="i"><input type="submit" name="submit" value="Print" /></span></td>
    </tr>
</table>

                                
</form></div></p>
			              </div>
							<ul class="pager">
								<?php
								//link ke halaman sebelumnya (previous)
								if($pg > 1){
									$previous=$pg-1;
									echo "<li><a href=$file&pg=1>&laquo; First</a></li>
									<li><a href=$file&pg=$previous>&laquo; Prev</a></li> ";
								}else{ 
									echo "<li class='disabled'>&laquo; First</li>
									<li class='disabled'>&laquo; Prev</li> ";
								}
								?>

								<?php
								//Link untuk Angka
								$angka=($pg > 3 ? " <li class='separator'>...</li> " : " ");
								for($i=$pg-2;$i<$pg;$i++)
								{
								  if ($i < 1) 
								      continue;
								  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
								}

								$angka .= " <li class='current'>$pg</li>";
								for($i=$pg+1;$i<($pg+3);$i++)
								{
								  if ($i > $jmlhalaman) 
								      break;
								  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
								}

								$angka .= ($pg+2<$jmlhalaman ? " <li class='separator'>...</li> <li><a href='$file&pg=$jmlhalaman'>$jmlhalaman</a></li>" : " ");

								echo "$angka";
								?>
								

								<?php
								//link kehalaman berikutnya (Next)
								if($pg < $jmlhalaman)
								{
									$next=$pg+1;
									echo "<li><a href='$file&pg=$next'>Next &raquo;</a></li>
									<li><a href='$file&pg=$jmlhalaman'>Last &raquo;</a></li>";
								}
								else
								{ 
									echo "<li class='disabled'>Next &raquo;</li>
									<li class='disabled'>Last &raquo;</li> ";
								}		
								?>													
							</ul>
						</div>

						<!-- end pagination -->
						<!-- table action -->
						
						<!-- end table action -->						
					</div>

<?php
}elseif ($act=='edit') {

	$id_bb = (int) filter($_GET['id']);

	$sqlSup = mysql_query("SELECT * FROM tb_bb WHERE id_bb=$id_bb");
	$dataSup = mysql_fetch_array($sqlSup);

	if(!empty($id_bb)){
		?>
		<!-- box / title -->
		<div class="title">
			<h5>Ubah Data Bahan Baku Masuk</h5>														
		</div>
		<!-- end box / title -->
		<?php
	}else{
		?>
		<!-- box / title -->
		<div class="title">
			<h5>Tambah Data Bahan Baku Masuk</h5>														
		</div>
		<!-- end box / title -->
		<?php
	}

?>	

	<div class="table">
		<form method="POST" action="?pages=bb_masuk&act=update">
		<input type="hidden" name="id_bb" value="<?php echo $dataSup['id_bb'];?>">
		<input type="hidden" name="jumlah_asal" value="<?php echo $dataSup['jumlah'];?>">
		<table style="width:auto">
		   	<tbody>
		   		<tr>
					<td><label>No <span class="left">Invoice</span></label></td>
					<td>:</td>
					<td><input type="text" value="<?php echo $dataSup['no_invoice'];?>" size="30" name="no_invoice" id="no_invoice" placeholder="No Invoice" required></td>
			   	</tr>
			   	<tr>
					<td><label>Nama Suplier</label></td>
					<td>:</td>
					<td>
                    <select class="select" style="width: 210px;" name="id_suplier" id="id_suplier" data-placeholder="Pilih Nama Suplier" required>
                      <option value=""></option>
                      <?php
                            $select_jur = mysql_query("SELECT * FROM tb_suplier");
                            while($select_jur2 = mysql_fetch_array($select_jur)){
							 if($dataSup['id_suplier']==$select_jur2['id_suplier']){
						  		$selected = " selected ";
							  }else{
							  	$selected = "";
							  }
                      ?>
                      <option <?php echo $selected;?> value="<?php echo "$select_jur2[id_suplier]";?>"><?php echo "$select_jur2[nama]";?></option>
                      <?php
                            }
                      ?>
                        </select>
                    </td>
			   	</tr>			   	
			   	<tr>
					<td style="vertical-align: top;"><label>Nama BB</label></td>
					<td>:</td>
					<td>
						<select name="id_kode" id="id_kode" data-placeholder="Pilih Kode Bahan Baku" style="width:210px">
						<option value=""></option>
					<?php 
					$sqlBB = mysql_query("SELECT * FROM tb_kode_bb");
					while($dataBB = mysql_fetch_array($sqlBB)){
						if($dataBB['kode']==$dataSup['kode_bb']){
							$selected = " selected ";
						}else{
							$selected = "";
						}
						echo '<option value="'.$dataBB['id_kode'].'" '.$selected.'>'.$dataBB['nama'].'</option>';
					}
					?>
					</select>
			   	</tr>
				<tr>
					<td><label>Kode BB</label></td>
					<td>:</td>
					<td><input type="text" value="<?php echo $dataSup['kode_bb'];?>" size="30" name="kode_bb" id="kode_bb" placeholder="kode bb" readonly="true"></td>
			   	</tr>
			   	<tr>
					<td><label>Jumlah</label></td>
					<td>:</td>
				 	<td><input type="text" value="<?php echo $dataSup['jumlah'];?>" size="30" name="jumlah" id="jumlah" placeholder="jumlah" required/></td> 
			   	</tr>
                  <td><label>Satuan</label></td>
					<td>:</td>
				 	<td><input type="text" value="<?php echo $dataSup['satuan'];?>" size="30" name="satuan" id="satuan" placeholder="satuan" readonly="true" required/></td> 
			   	</tr>
                  <td><label>Tgl Masuk</label></td>
					<td>:</td>
				 	<td><input type="text"  value="<?php echo $dataSup['tgl_masuk'];?>" size="30" name="tgl_masuk" id="tgl_masuk" placeholder="tgl_masuk" required/></td> 
			   	</tr>
                  <td style="vertical-align:top"><span class="left"><label>Keterangan</label></span></td>
					<td style="vertical-align:top">:</td>
				 	<td><textarea style="width:400px;height:80px;" name="ket" id="ket" placeholder="Keterangan"><?php echo $dataSup['ket'];?></textarea></td> 
			   	</tr>
			 	<tr>
			 		<td>&nbsp;</td>
			 		<td>&nbsp;</td>
			 		<td>
			 			<input type="hidden" value="" size="15" name="id">
						<input type="submit" value="Simpan" name="submit" class="btn btn-primary">
						<input type="Reset" value="Reset" class="btn">
			 		</td>
		   		</tr>
		 	</tbody>
		</table>
		</form>
	</div>
<?php
}elseif ($act=='update') {
	# code...
	$id_bb = (int) $_POST['id_bb'];
	$no_invoice = $_POST['no_invoice'];
	$id_suplier = $_POST['id_suplier'];	
	$jumlah = $_POST['jumlah'];
	$satuan = $_POST['satuan'];
	$tgl_masuk = $_POST['tgl_masuk'];
	$ket = $_POST['ket'];
	$kode_bb = $_POST['kode_bb'];
	$jumlah_asal = $_POST['jumlah_asal'];

	$dataBB = mysql_fetch_array(mysql_query("SELECT stock_bb, nama FROM tb_kode_bb WHERE kode='$kode_bb'"));
	$stock_bb = $dataBB['stock_bb'];
	$nama_bb = $dataBB['nama'];


	if(!empty($id_bb)){
		$update = mysql_query("UPDATE tb_bb SET no_invoice='$no_invoice', id_suplier='$id_suplier', nama_bb='$nama_bb', jumlah='$jumlah', satuan='$satuan', tgl_masuk='$tgl_masuk', ket='$ket', kode_bb='$kode_bb' WHERE id_bb=$id_bb");

		$totalBB = ($stock_bb - $jumlah_asal) + $jumlah;
		mysql_query("UPDATE tb_kode_bb SET stock_bb='$totalBB' WHERE kode='$kode_bb'");

		if($update)
			sukses('Data berhasil disimpan', '?pages=bb_masuk');
	}else{		

		$insert = mysql_query("INSERT INTO tb_bb VALUES(NULL, '$no_invoice', '$id_suplier', '$nama_bb', '$jumlah', '$satuan', '$tgl_masuk', '$ket', '$kode_bb')");

		$totalBB = $stock_bb + $jumlah;
		mysql_query("UPDATE tb_kode_bb SET stock_bb='$totalBB' WHERE kode='$kode_bb'");

		if($insert)
			sukses('Data berhasil disimpan', '?pages=bb_masuk');
	}
	
}elseif ($act=='del') {
	# code...
	$id_bb = (int) filter($_GET['id']);

	$sqlSup = mysql_query("SELECT * FROM tb_bb WHERE id_bb=$id_bb");
	$dataSup = mysql_fetch_array($sqlSup);
	$kode_bb = $dataSup['kode_bb'];
	$jumlah = $dataSup['jumlah'];

	$dataBB = mysql_fetch_array(mysql_query("SELECT stock_bb FROM tb_kode_bb WHERE kode='$kode_bb'"));
	$stock_bb = $dataBB['stock_bb'];	

	$totalBB = $stock_bb-$jumlah;

	mysql_query("UPDATE tb_kode_bb SET stock_bb='$totalBB' WHERE kode='$kode_bb'");

	$delete = mysql_query("DELETE FROM tb_bb WHERE id_bb=$id_bb");

	if($delete)
		sukses('Data berhasil dihapus', '?pages=bb_masuk');
}
?>		
<script type="text/javascript">
$(document).ready(function () {
	$("#id_kode, #id_suplier").chosen();  
})
</script>				